package com.jason.quectel.activities;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.text.Editable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.jason.quectel.R;
import com.jason.quectel.utils.FileUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.concurrent.Executors;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class FileActivity extends AppCompatActivity {
    private static final String TAG = "FileActivity";
    private EditText etContent;
    private TextView tvShow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file);

        File filesDir = getFilesDir();
        File cacheDir = getCacheDir();

        File externalCacheDir = getExternalCacheDir();
        File externalFilesDir = getExternalFilesDir("");

        Log.d(TAG, "getFilesDir(): " + filesDir.getAbsolutePath());
        Log.d(TAG, "getCacheDir(): " + cacheDir.getAbsolutePath());
        Log.d(TAG, "getExternalCacheDir(): " + externalCacheDir.getAbsolutePath());
        Log.d(TAG, "getExternalFilesDir(): " + externalFilesDir.getAbsolutePath());

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
                File externalStorageDirectory1 = Environment.getExternalStorageDirectory();
                Log.d(TAG, "getExternalStorageDirectory(): " + externalStorageDirectory1.getAbsolutePath());
            } else {
                requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1001);
            }
        }

        etContent = findViewById(R.id.et_content);
        tvShow = findViewById(R.id.tv_show);
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("name", "jason");
            jsonObject.put("age", "29");
            String s = jsonObject.toString();
            Log.d(TAG, "jsonObject: " + s);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1001 && resultCode == RESULT_OK) {
            File externalStorageDirectory1 = Environment.getExternalStorageDirectory();
            Log.d(TAG, "getExternalStorageDirectory(): " + externalStorageDirectory1.getAbsolutePath());
        }
    }

    public void save(View view) {
        Editable content = etContent.getText();
        if (TextUtils.isEmpty(content)) {
            Toast.makeText(this, "请先输入内容", Toast.LENGTH_SHORT).show();
            return;
        }
        Executors.newCachedThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                File externalFilesDir = getExternalFilesDir("");
                FileUtils.text2File(content.toString(), externalFilesDir.getAbsolutePath() + File.separator + "test.txt");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(FileActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }

    public void read(View view) {
        Executors.newCachedThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                File externalFilesDir = getExternalFilesDir("");
                String content = FileUtils.readFile(externalFilesDir.getAbsolutePath() + File.separator + "test.txt");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tvShow.setText(content);
                    }
                });
            }
        });
    }
}